//
//  CircleChartView.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/19.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "CircleChartView.h"
#import <Masonry/Masonry.h>
#import "PNChart.h"


@interface CircleChartView ()
@property (strong, nonatomic) PieChartView *pieChartView;
@property (nonatomic, strong) NSArray *valueArr;//保存圆形的值
@property (nonatomic, strong) NSArray *titleArr;//保存圆形的title
@property (nonatomic, strong) NSMutableArray *colorArr;//保存圆形的颜色

@end

@implementation CircleChartView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.pieChartView =  [[PieChartView alloc] init];
        [self insertSubview:self.pieChartView atIndex:0];
        [self.pieChartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.pieChartView = [[PieChartView alloc] init];
        [self insertSubview:self.pieChartView atIndex:0];
        [self.pieChartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self configureTopCirCleVie];
}

#pragma mark - CircleView
- (void)configureTopCirCleVie {
//    _pieChartView.delegate = self;
    _pieChartView.usePercentValuesEnabled = YES;
    //_pieChartView.holeTransparent = YES;
    _pieChartView.drawHoleEnabled = YES;
    _pieChartView.holeColor = [UIColor clearColor];
    //    _pieChartView.centerTextFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
    _pieChartView.holeRadiusPercent = 0.58;
    _pieChartView.transparentCircleRadiusPercent = 0.61;
    _pieChartView.descriptionText = @"";
    _pieChartView.drawCenterTextEnabled = NO;
    _pieChartView.drawHoleEnabled = YES;
    _pieChartView.rotationAngle = 0.0;
    _pieChartView.rotationEnabled = YES;
    _pieChartView.holeColor = [UIColor whiteColor];
    _pieChartView.highlightPerTapEnabled = NO;
    
    NSArray *legendTitleArr = @[NSLocalizedStringFromTable(@"Clear", @"Location", nil),NSLocalizedStringFromTable(@"Smash",@"Location",nil),NSLocalizedStringFromTable(@"Drive",@"Location",nil),NSLocalizedStringFromTable(@"Block",@"Location",nil),NSLocalizedStringFromTable(@"Lift",@"Location",nil),NSLocalizedStringFromTable(@"Slice",@"Location",nil)];
    NSArray *lengendColorArr = @[
                                 [UIColor colorWithRed:247/255.0 green:179/255.0 blue:94/255.0 alpha:1.0],
                                 [UIColor colorWithRed:190/255.0 green:69/255.0 blue:86/255.0 alpha:1.0],
                                 [UIColor colorWithRed:152/255.0 green:202/255.0 blue:65/255.0 alpha:1.0],
                                 [UIColor colorWithRed:244/255.0 green:144/255.0 blue:186/255.0 alpha:1.0],
                                 [UIColor colorWithRed:81/255.0 green:170/255.0 blue:207/255.0 alpha:1.0],
                                 [UIColor colorWithRed:94/255.0 green:201/255.0 blue:149/255.0 alpha:1.0]
                                 ];
    
    ChartLegend *l = _pieChartView.legend;
    [l setCustomWithColors:lengendColorArr labels:legendTitleArr];
    l.position = ChartLegendPositionRightOfChart;
    l.xEntrySpace = 7.0;
    l.yEntrySpace = 2.0;
    l.yOffset = 0.0;
    l.textColor = [UIColor whiteColor];
    l.enabled = NO;
    l.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
    l.verticalAlignment = ChartLegendVerticalAlignmentTop;
    l.orientation = ChartLegendOrientationVertical;
}

- (void)configureDataArray:(NSArray *)dataArray titleArray:(NSArray *)titleArray colorArray:(NSArray *)colorArray isPresent:(BOOL)isPresent withAnimationDur:(NSTimeInterval)animationDuration {
    
    
    
    _valueArr = dataArray;
    _titleArr = titleArray;
    _colorArr = [colorArray mutableCopy];
    [self setDataCount:_valueArr.count isPresent:isPresent];
    if (animationDuration > 0) {
        [_pieChartView animateWithXAxisDuration:animationDuration yAxisDuration:animationDuration easingOption:ChartEasingOptionEaseOutBack];
    }
}


- (void)setDataCount:(NSInteger)count isPresent:(BOOL)isPresent {
    
    if (count <= 0) {
        return;
    }
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    //小于5%时不显示文字
    NSInteger allSwingcout = 0;
    for (NSNumber *swingCount in _valueArr) {
        allSwingcout += [swingCount integerValue];
    }
    
    for (NSInteger i = 0; i < count; i++) {
        CGFloat valueT = [_valueArr[i] floatValue];
        if (valueT != 0) {
            NSString *labelStr = _titleArr[i];
            if (valueT/allSwingcout <= 0.05) {
                labelStr = @"";
            }
            [yVals1 addObject:[[PieChartDataEntry alloc] initWithValue:[_valueArr[i] integerValue] label:labelStr]];
        }else {
            [_colorArr replaceObjectAtIndex:i withObject:[UIColor clearColor]];
        }
    }
    [_colorArr removeObject:[UIColor clearColor]];
    
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:yVals1 label:@"nil"];
    dataSet.sliceSpace = 0.0;
    dataSet.selectionShift = 9;
    dataSet.colors = _colorArr;
    
    PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
    NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
    pFormatter.roundingMode = kCFNumberFormatterRoundHalfEven;
    pFormatter.maximumFractionDigits = 1;
    pFormatter.multiplier = @1.f;
    
    if (isPresent) {
        pFormatter.numberStyle = NSNumberFormatterPercentStyle;
        pFormatter.percentSymbol = @" %";
        self.pieChartView.usePercentValuesEnabled = YES;
    }else {
        pFormatter.numberStyle = NSNumberFormatterNoStyle;
        _pieChartView.usePercentValuesEnabled = NO;
    }
    [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:pFormatter]];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];
    [data setValueTextColor:UIColor.whiteColor];
    
    _pieChartView.data = data;
    
    [_pieChartView highlightValues:nil];
    
}



- (void)configureDataArray:(NSArray *)dataArray titleArray:(NSArray *)titleArray colorArray:(NSArray *)colorArray isPresent:(BOOL)isPresent
{
    [self configureDataArray:dataArray titleArray:titleArray colorArray:colorArray isPresent:isPresent withAnimationDur:1.5];
}

@end



